﻿using System.Collections.Generic;
using System.Linq;
using MSAcademic.v2.Database;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Infrastructure.Gallery.Impls
{
    public class GalleryQueriesImpl : IGalleryQueries
    {

        public IGallery GetGallery(int galleryId)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var gallery = db.Galleries.SingleOrDefault(m => m.Id == galleryId);
                if (gallery != null)
                    return new GalleryImpl(gallery);
            }
            return null;
        }
        public IEnumerable<IGallery> GetAllGalleries(IUser signedInUser)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {

                return db.Galleries
                        .Where(x => x.Article.UserId == signedInUser.Id) 
                        .Select(gallery => new GalleryImpl
                        {
                            Name = gallery.Name,
                            Id = gallery.Id,
                            IsHidden = gallery.IsHidden
                        }).ToList();
            }
        }
        public IGallery GetGalleryForArticle(int articleId)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var gallery = db.Galleries.SingleOrDefault(m => m.Article.Id == articleId);
                if (gallery != null)
                {
                    return new GalleryImpl(gallery);
                }
                return null;

            }
        }

    }


}
